/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.Graphics;
import java.util.ArrayList;

import soccerscope.model.ClientScene;
import soccerscope.model.Param;
import soccerscope.model.PlayMode;
import soccerscope.model.Player;
import soccerscope.model.SceneSet;
import soccerscope.model.WorldModel;
import soccerscope.util.geom.Point2f;
import soccerscope.view.FieldPane;

public class PassCourseLayer extends Layer {
	private static Player nowKicker, nextKicker;

	private static ArrayList kickPointList;

	// private static int nowTime, nextTime;

	public PassCourseLayer(FieldPane fieldPane, boolean enable) {
		super(fieldPane, enable);
		kickPointList = new ArrayList();
		nowKicker = null;
		nextKicker = null;
		// nowTime = 0;
		// nextTime = 0;
	}

	@Override
	public String getLayerName() {
		return "PassCourse";
	}

	@Override
	public void draw(Graphics g) {
		if (!this.enable)
			return;
		ClientScene clientScene = this.fieldPane.getScene();
		float ballR = Param.BALL_SIZE * this.fieldPane.getBallMagnify();
		WorldModel wm = WorldModel.getInstance();
		SceneSet sceneSet = wm.getSceneSet();

		int time = clientScene.time;
		if (sceneSet.hasScene(time + 1)) {
			/* check now kicker */
			for (int i = 0; i < Param.MAX_PLAYER * 2; i++) {
				if (!clientScene.player[i].isEnable())
					continue;
				if (clientScene.player[i].isKicking()
						|| clientScene.player[i].isCatching()) {
					nowKicker = clientScene.player[i];
					// nowTime = time;
				}
			}
			/* check next kicker */
			boolean isKicked = false;
			while (!isKicked) {
				if (time >= sceneSet.getLimitTime())
					break;
				time++;
				ClientScene tmpClientScene = (ClientScene) sceneSet
						.getScene(time);
				for (int i = 0; i < Param.MAX_PLAYER * 2; i++) {
					if (tmpClientScene.player[i].isKicking()
							|| tmpClientScene.player[i].isCatching()) {
						isKicked = true;
						nextKicker = tmpClientScene.player[i];
						// nextTime = time;
					}
				}
			}
		}

		/*
		 * Point2f p1 = sceneSet.getScene(nowTime).ball.pos; for (int i =
		 * nowTime; i < nextTime; i++) { Point2f p2 =
		 * sceneSet.getScene(i).ball.pos; g.setColor(nowKicker.getColor());
		 * drawLine(g, p1, p2); p1 = p2; }
		 */

		if ((nowKicker != null && nextKicker != null)
				&& (clientScene.pmode.pmode == PlayMode.PM_PlayOn)) {
			if (nowKicker.side == nextKicker.side) {
				kickPointList.add(new TwoPoints(nowKicker.pos, nextKicker.pos));
				g.setColor(nowKicker.getColor());
				for (int i = 0; i < kickPointList.size(); i++) {
					drawArrow(g, ((TwoPoints) (kickPointList.get(i))).p1,
							((TwoPoints) (kickPointList.get(i))).p2);
				}
			} else {
				kickPointList.clear();
			}
		} else
			kickPointList.clear();
	}

	class TwoPoints {
		Point2f p1, p2;

		TwoPoints(Point2f p1, Point2f p2) {
			this.p1 = p1;
			this.p2 = p2;
		}
	}
}
